package com.source.multithread.run;


/**
 * @Author wengqy
 * @Date 2020/12/19
 * 龟兔赛跑
 */
public class ToriseAndRabbitRace implements Runnable{
    public static void main(String[] args) {
      new Thread(new ToriseAndRabbitRace(),"兔子").start();
      new Thread(new ToriseAndRabbitRace(),"乌龟").start();
    }


  private static  String winner ;
    @Override
    public void run() {
        for (int i = 1; i <=100 ; i++) {
            if(Thread.currentThread().getName().equals("兔子")&&i%20==0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            boolean flag = gameover(i);
            if(flag){
               break;
            }
            System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
        }
    }

    private boolean gameover(int step) {
        if(winner!=null){
           return true;
        }
        if(step>=100){
            winner = Thread.currentThread().getName();
            System.out.println("冠军是："+winner);
            return true;

        }
        return false;

    }
}
